package wang.unclecat.aar2jar


import org.gradle.api.Plugin
import org.gradle.api.Project

//https://www.jianshu.com/p/afa35b6a1098  如何实现provided AAR？如何实现Jar依赖AAR
class Aar2jarPlugin implements Plugin<Project> {


  @Override
  void apply(Project project) {
    println "Aar2jarPlugin --->"
    project.configurations.compileOnly.canBeResolved = true

//        project.dependencies {
//            registerTransform {
//                from.attribute(ArtifactAttributes.ARTIFACT_FORMAT, "aar")
//                to.attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.JAR_TYPE)
//                artifactTransform(AarToJarTransform::class.java)
//            }
//        }

    project.afterEvaluate {
      project.dependencies {

        ExtractAarTransform extractAarTransform = new ExtractAarTransform()

        def cachePath = "pitaya-caches/aar2jar"
        extractAarTransform.setOutputDirectory(new File(project.buildDir, cachePath))


        project.configurations.compileOnly.files
            .findAll { it.name.endsWith("aar") }
            .collect {
              List<File> tempRoot = extractAarTransform.transform(it.absoluteFile)
              List<File> tempAAR2JarFile = extractAarTransform.getJars(tempRoot.get(0))

              project.dependencies.add("api", project.files(tempAAR2JarFile.toArray()))

              println("From AAR ===> ${it.absolutePath} \nTo Jar===>${tempAAR2JarFile} ")
            }
      }
    }

  }
}

